package org.wangpf.service.common;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.wangpf.util.SimpleHashMap;
/**
 * sql查询封装成自己的vo对象所需要的转换器,vo对象可以定义long int String Date
 * @author wangpf
 *
 */
public class VoORTransform implements IORTransform {
	private Class<?> pojo;
	private Map<String, Method> methods = new HashMap<String, Method>();
	private Map<String, Field> fields = new HashMap<String, Field>();

	public VoORTransform(Class<?> mapClassInfo) {
		this.pojo = mapClassInfo;

		java.lang.reflect.Field[] arrField = mapClassInfo.getDeclaredFields();
		for (Field field : arrField) {
			fields.put(field.getName().toLowerCase(), field);
		}

	}

	public Object transform(long rowIndex, SimpleHashMap arg1) {

		Object obj;
		try {
			obj = pojo.newInstance();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		Set<?> keys = arg1.keySet();
		String fieldName="";
		for (Object key : keys) {
			fieldName=key.toString();
			if(fields.containsKey(fieldName.toLowerCase())){
				Field f=fields.get(fieldName.toLowerCase());
				f.setAccessible(true);
				try {
				/*	
					if(f.getType().isAssignableFrom(Date.class)){
						PropertyUtils.setProperty(obj, f.getName(), arg1.getDate(fieldName));
					}else if(f.getType().isAssignableFrom(long.class)){
						PropertyUtils.setProperty(obj, f.getName(), arg1.getDate(fieldName));
					}else if(f.getType().isAssignableFrom(int.class)){
						PropertyUtils.setProperty(obj, f.getName(), arg1.getInt(fieldName));
					}else if(f.getType().isAssignableFrom(Integer.class)){
						PropertyUtils.setProperty(obj, f.getName(), Integer.valueOf(arg1.getInt(fieldName)));
					}else{
						PropertyUtils.setProperty(obj, f.getName(), arg1.get(fieldName));
					}
					*/
					PropertyUtils.setProperty(obj, f.getName(), arg1.get(fieldName));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

		return obj;
	}
}
